package com.xi.controller;

import com.xi.pojo.BookTypeCount;
import com.xi.pojo.Result;
import com.xi.service.BookService;
import com.xi.service.BorrowService;
import com.xi.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping
public class ShowController {
    @Autowired
    private BorrowService borrowService;

    @Autowired
    private UserService userService;

    @Autowired
    private BookService bookService;

    /*
     * 展示借阅总数
     * */
    @GetMapping("/borrowCount")
    public Result showBorrowCount() {
        log.info("展示借阅总数");
        Integer count = borrowService.count();
        return Result.success(count);
    }

    /*
     * 展示逾期数量
     * */
    @GetMapping("/overdueCount")
    public Result showOverdueCount() {
        log.info("展示逾期数量");
        Integer count = 10;
        return Result.success(count);
    }

    /*
     * 展示注册用户总数
     * */
    @GetMapping("/userCount")
    public Result showUserCount() {
        log.info("展示注册用户总数");
        Integer count = userService.count();
        return Result.success(count);
    }

    /*
     * 获取借阅列表中图书类型最多的图书类型
     * */
    @GetMapping("/mostClassify")
    public Result showMostClassify() {
        log.info("获取借阅列表中图书类型最多的图书类型");
        return Result.success(borrowService.mostClassify());
    }

    /*
     * 获取最多借阅次数的用户名
     * */
    @GetMapping("/mostUser")
    public Result showMostUser() {
        log.info("获取最多借阅次数的用户名");
        return Result.success(borrowService.mostUser());
    }

    /*
     * 获取得到逾期率
     * */
    @GetMapping("/overdueRate")
    public Result showOverdueRate() {
        log.info("获取得到逾期率");
        //1.获取逾期数
        Integer overdueCount = 10;

        //2.获取总借阅数
        Integer borrowCount = borrowService.count();

        //3.获取逾期率,保留两位小数
        Double overdueRate = (double) overdueCount / borrowCount;

        //4.进行拼接
        String overdueRateStr = String.format("%.2f", overdueRate) + "%";

        return Result.success(overdueRateStr);
    }

    /*
    * 展示分类饼状图
    * */
    @GetMapping("/book/type/count")
    public Result getBookTypeCount() {
        log.info("展示分类饼状图");
        List<BookTypeCount> list = bookService.listTypeCount();
        return Result.success(list);
    }
}
